package com.sxpcwlkj.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxpcwlkj.common.utils.MapstructUtil;
import com.sxpcwlkj.system.entity.SysDept;
import com.sxpcwlkj.system.entity.SysUser;
import com.sxpcwlkj.system.entity.bo.SysDeptBo;
import com.sxpcwlkj.system.entity.vo.SysDeptVo;
import com.sxpcwlkj.common.enums.SystemCommonEnum;
import com.sxpcwlkj.system.mapper.SysDeptMapper;
import com.sxpcwlkj.system.mapper.SysUserMapper;
import com.sxpcwlkj.system.service.SysDeptService;
import com.sxpcwlkj.common.utils.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName SysDeptServiceImpl
 * @Description TODO
 * @Author 西决
 * @Date 2022/12/4 0:08
 */
@RequiredArgsConstructor
@Service("sysDept")
@Slf4j
public class SysDeptServiceImpl implements SysDeptService {

    private final SysDeptMapper baseMapper;

    private final SysUserMapper sysUserMapper;

    @Override
    public List<SysDeptVo> queryTree() {
        List<SysDeptVo> sysDepts = baseMapper.selectVoList(new LambdaQueryWrapper<SysDept>().eq(SysDept::getStatus, SystemCommonEnum.SYS_COMMON_STATE_OPEN.getValue()).orderByAsc(SysDept::getSort));
        return selectFids(sysDepts, 0L);
    }

    @Override
    public List<SysDeptVo> queryTreeAll() {
        List<SysDeptVo> sysDepts = baseMapper.selectVoList(new LambdaQueryWrapper<SysDept>().orderByAsc(SysDept::getSort));
        return selectFids(sysDepts, 0L);
    }


    @Override
    public List<SysDeptVo> queryListSon(Long id, List<SysDeptVo> endList) {
        SysDeptVo sysDept = baseMapper.selectVoById(id);
        if (sysDept != null) {
            endList.add(sysDept);
            queryListSon(sysDept.getParentId(), endList);
        }
        return endList;
    }

    /**
     * 查询下级结构
     *
     * @param deptVos
     * @param fid
     * @return
     */
    private List<SysDeptVo> selectFids(List<SysDeptVo> deptVos, Long fid) {
        List<SysDeptVo> endList = new ArrayList<>();
        for (SysDeptVo s : deptVos) {
            if (fid.equals(s.getParentId())) {
                List<SysDeptVo> vo = selectFids(deptVos, s.getDeptId());
                s.setChildren(vo);
                endList.add(s);
            }
        }
        return endList;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param deptId 主键
     * @return 实例对象
     */
    public SysDeptVo queryById(Long deptId) {
        SysDeptVo sysDeptVo = baseMapper.selectVoById(deptId);
        if (sysDeptVo != null) {
            // 部门
            List<SysDeptVo> list = new ArrayList<>();
            this.queryListSon(deptId, list);
            list.remove(0);
            Collections.reverse(list);
            sysDeptVo.setDeptIds(list.stream().map(SysDeptVo::getDeptId).toArray(Long[]::new));
        }
        return sysDeptVo;
    }


    /**
     * 新增数据
     *
     * @return 实例对象
     */
    public Boolean insert(SysDeptBo bo) {
        int row = 0;
        SysDept obj = MapstructUtil.convert(bo, SysDept.class);
        obj.setDeptId(null);
        row = baseMapper.insert(obj);
        return row > 0;
    }

    /**
     * 更新数据
     *
     * @param sysDept 实例对象
     * @return 实例对象
     */
    public Boolean update(SysDeptBo sysDept) {
        int row = 0;
        SysDept convert = MapstructUtil.convert(sysDept, SysDept.class);
        row = baseMapper.updateById(convert);
        return row > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param deptId 主键
     * @return 是否成功
     */
    public Boolean deleteById(Long deptId) {
        //查询是否有用户在这个部门
        Long count = sysUserMapper.selectCount(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId, deptId));
        if (count > 0) {
            throw new SecurityException("当前部门存在用户，不能删除!");
        }
        int total = baseMapper.deleteById(deptId);
        return total > 0;
    }


}
